/**
 * Created by gongxun on 16/11/19.
 */

var KwxCardHelper = function(){
    var that = new MjCardPlace();

    that.allBasicLawCardNums = null;

    that.selectPiaoIndex = null;

    //设置牌桌缩放相关 初始化
    that.setCardScaleInfo = function(){
        that.init();

        //牌背景
        var cardIndex = mo.fileHelper.getItem("gameKwxCardIndex_key");
        if (cardIndex == undefined){
            mo.gameUserInfo.mGameKwxCardIndex = "1";
        }else{
            mo.gameUserInfo.mGameKwxCardIndex = cardIndex;
        }
        var tableBgIndex = mo.fileHelper.getItem("gameKwxBgIndex_key");
        if (tableBgIndex == undefined){
            mo.gameUserInfo.mGameKwxBgIndex = "1";
        }else{
            mo.gameUserInfo.mGameKwxBgIndex = tableBgIndex;
        }

        //手牌可能性
        that.allBasicLawCardNums = [];
        for (var i = 0; i < 34; i++){
            var num = i + 1;
            if (num <= 9)
            {
                num += 20;
            }
            else if (num <= 18)
            {
                num += (40 - 9);
            }
            else if (num <= 27)
            {
                num += (60 - 18);
            }
            else
            {
                num += (80 - 27);
            }
            that.allBasicLawCardNums.push(num);
        }

        //默认选择的飘
        var piao = mo.fileHelper.getItem("gameKwxPiao_key");
        if (piao == undefined){
            that.selectPiaoIndex = 0;
        }else{
            that.selectPiaoIndex = parseInt(piao);
        }
    };

    //用户准备时候选的飘
    that.getUseSelectPiao = function(){
        if (mo.kwxTable.mTableConfig.piao == 0){
            return 0;
        }else{
            return that.selectPiaoIndex;
        }
    };

    //用户改变飘
    that.setUserSelectPiao = function(piao){
        that.selectPiaoIndex = piao;
        mo.fileHelper.setItem("gameKwxPiao_key", piao + "");
        mo.eventHelper.dispatch("gameSelectPiaoUpdate");
    };

    //返回开口牌结构
    that.getOpenCardStruct = function(cards, type){
        var openCard = {};

        openCard.cards = cards;
        openCard.type = type;

        return openCard;
    };

    //返回手牌size
    that.getInhnadCardSize = function(){
        var cardSize = that.inhandCardSize;
        var cardSize = cc.size(cardSize.width * that.inhandCardScale, cardSize.height * that.inhandCardScale);
        return cardSize;
    };

    //排序手牌
    that.sortCardForNum = function(cards){
        cards.sort(function(a, b){
            //让亮出来的牌放在前面
            if (a.mCardNum == 0 && b.mCardNum != 0){
                return 1;
            }else if (a.mCardNum != 0 && b.mCardNum == 0){
                return -1;
            }

            //手牌num大的放在后面 如果一样就比较生成的id 防止跳牌
            var temp = (a.mCardNum - b.mCardNum);
            if (temp == 0){
                return a.mCreatIndex - b.mCreatIndex;
            }
            return temp;
        });
    };

    //排序num
    that.sortForNum = function(nums){
        nums.sort(function(a, b){
            //手牌num大的放在后面 如果一样就比较生成的id 防止跳牌
            var temp = (a - b);
            return temp;
        });
    };

    //排序手牌 数组中的排到最前面
    that.sortCardForNumArray = function(cards){
        cards.sort(function(a, b){
            //让亮出来的牌放在前面
            var aCunzai = a.mIsLiangCard;
            var bCunzai = b.mIsLiangCard;
            if (aCunzai && !bCunzai){
                return -1;
            }else if (!aCunzai && bCunzai){
                return 1;
            }

            //手牌num大的放在后面 如果一样就比较生成的id 防止跳牌
            var temp = (a.mCardNum - b.mCardNum);
            if (temp == 0){
                return a.mCreatIndex - b.mCreatIndex;
            }
            return temp;
        });
    };

    //返回随机手牌
    that.getRandomCardNum = function(size){
        var cards = [];
        for (var i = 0; i < size; i++){
            var num = parseInt(Math.random() * 34) + 1;
            if (num <= 9)
            {
                num += 20;
            }
            else if (num <= 18)
            {
                num += (40 - 9);
            }
            else if (num <= 27)
            {
                num += (60 - 18);
            }
            else
            {
                num += (80 - 27);
            }
            cards.push(num);
        }
        return cards;
    };

    //返回手牌坐标
    that.getInhandCardPos = function(seat, index, openCardSize, isFromCard){
        //2人场对面玩家坐标
        if (mo.gameMsgManager.kwxGameModel == 2 && seat != 0){
            var offset = that.oppoInhandCardOffset;
            var cardSize = cc.size(that.inhandCardSize.width * that.oppoInhandCardScale, that.inhandCardSize.height * that.oppoInhandCardScale);
            var xStart = mo.gameSize.x - (mo.gameSize.x - (cardSize.width + offset) * 14) / 2 - cardSize.width / 2;
            var lastCardAdd = (isFromCard ? 20 : 0);
            var x = xStart - (cardSize.width + offset) * (index + openCardSize * 3 + 0.5) - lastCardAdd;
            return cc.p(x, mo.gameSize.y - 15 - cardSize.height);
        }

        if (seat == 0){
            var xStart = that.inhandCardJiange;
            var offset = that.inhandCardOffset;
            var cardSize = cc.size(that.inhandCardSize.width * that.inhandCardScale, that.inhandCardSize.height * that.inhandCardScale);
            var lastCardAdd = (isFromCard ? that.inhandCardJiange * 2 / 3 : 0);
            var x = xStart + (cardSize.width + offset) * (index + openCardSize * 3 + 0.5) + lastCardAdd;
            return cc.p(x, 15);
        }else if (seat == 1){
            var widthOffset = that.leftInhandRect.x;
            var xStart = mo.gameSize.x - widthOffset;
            var yAdd = (that.leftInhandCardSize.height + that.leftInhandCardOffset) * that.leftInhandCardScale;
            var yStart = that.leftInhandRect.y;
            var lastCardAdd = (isFromCard ? that.leftInhandCardOffsetLast : 0);
            var y = yStart + yAdd * (index + openCardSize * 3) + lastCardAdd;
            return cc.p(xStart, y);
        }else if (seat == 2){
            var widthOffset = that.leftInhandRect.x;
            var xStart = widthOffset;
            var yAdd = (that.leftInhandCardSize.height + that.leftInhandCardOffset) * that.leftInhandCardScale;
            var yStart = that.leftInhandRect.y + that.leftInhandRect.height - yAdd;
            var lastCardAdd = (isFromCard ? that.leftInhandCardOffsetLast : 0);
            var y = yStart - yAdd * (index + openCardSize * 3) - lastCardAdd;
            return cc.p(xStart, y);
        }
    };

    //返回打出去的牌的坐标
    that.getDealCardPos = function(seat, index){
        var count = 8;
        if (mo.gameSize.x / mo.gameSize.y <= 1000 / 640 && seat == 0){
            count = 6;
        }
        //2人场重新调整
        if (mo.gameMsgManager.kwxGameModel == 2){
            count = count + count;
        }

        var row = parseInt(index / count);
        var col = index % count;

        var xStart = 0;
        var cardSize = cc.size(0, 0);
        var yStart = 0;

        //2人场对面玩家坐标
        if (mo.gameMsgManager.kwxGameModel == 2 && seat != 0){
            cardSize = cc.size(that.inhandCardSize.width * that.dealScale, that.inhandCardSize.height * that.dealScale);
            xStart = mo.gameSize.x - (mo.gameSize.x / 2 - (cardSize.width * count / 2) + cardSize.width / 2);
            var yAdd = 56;
            yStart = mo.gameSize.y - 230;
            return cc.p(xStart - col * cardSize.width, yStart + row * yAdd);
        }

        if (seat == 0){
            cardSize = cc.size(that.inhandCardSize.width * that.dealScale, that.inhandCardSize.height * that.dealScale);
            xStart = mo.gameSize.x / 2 - (cardSize.width * count / 2) + cardSize.width / 2;
            var yAdd = 56;
            yStart = 230;
            return cc.p(xStart + col * cardSize.width, yStart + row * yAdd);
        }else if(seat == 1){
            cardSize = cc.size(that.leftOpenCardSize.width * that.leftDealScale, that.leftOpenCardSize.height * that.leftDealScale);
            xStart = mo.gameSize.x - 250;
            var yAdd = 36;
            yStart = 230;
            return cc.p(xStart - row * cardSize.width, yStart + col * yAdd);
        }else if(seat == 2){
            cardSize = cc.size(that.leftOpenCardSize.width * that.leftDealScale, that.leftOpenCardSize.height * that.leftDealScale);
            xStart = 250;
            var yAdd = 36;
            yStart = 230 + yAdd * 7;
            return cc.p(xStart + row * cardSize.width, yStart - col * yAdd);
        }
    };

    //返回开口牌坐标
    that.getOpenCardPos = function(seat, index, inIndex){
        //2人场对面玩家坐标
        if (mo.gameMsgManager.kwxGameModel == 2 && seat != 0){
            var startPos = that.getInhandCardPos(seat, 0, index, false);
            startPos.y += 5;
            var openScale = that.oppoOpenCardScale;
            var openOffset = 0;
            var xAdd = 0;
            var yAdd = 0;
            if (inIndex == 3){
                xAdd = (that.inhandCardSize.width * openScale + openOffset) * 1;
                yAdd = 34 * openScale;
            }else{
                xAdd = (that.inhandCardSize.width * openScale + openOffset) * inIndex;
            }
            return cc.p(startPos.x - xAdd, startPos.y + yAdd);
        }

        if (seat == 0){
            var startPos = that.getInhandCardPos(seat, 0, index, false);
            startPos.y += 5;
            var openScale = that.openCardScale;
            var openOffset = 0;
            var xAdd = 0;
            var yAdd = 0;
            if (inIndex == 3){
                xAdd = (that.inhandCardSize.width * openScale + openOffset) * 1;
                yAdd = 34 * openScale;
            }else{
                xAdd = (that.inhandCardSize.width * openScale + openOffset) * inIndex;
            }
            return cc.p(startPos.x + xAdd, startPos.y + yAdd);
        }else if (seat == 1){
            var pos = that.getInhandCardPos(seat, 0, index, false);
            var startY = pos.y;
            var x = pos.x;
            var y = 0;
            var scale = that.leftOpenCardScale;
            if (inIndex == 3){
                y = startY + 1 * 96 * scale + 33 * scale;
            }else{
                y = startY + inIndex * 96 * scale;
            }
            if (index > 0){
                y -= 6;
            }
            return cc.p(x, y);
        }else if (seat == 2){
            var pos = that.getInhandCardPos(seat, 0, index, false);
            var startY = pos.y;
            var x = pos.x;
            var y = 0;
            var scale = that.leftOpenCardScale;
            if (inIndex == 3){
                y = startY - 1 * 96 * scale + 33 * scale;
            }else{
                y = startY - inIndex * 96 * scale;
            }
            if (index > 0){
                y += 6;
            }
            return cc.p(x, y);
        }

        return cc.p(0, 0);
    };

    //返回是否存在
    that.findCardForNum = function(cards, cardNum){
        var rIndex = null;
        for (var i = 0; i < cards.length; i++){
            if (cards[i].mCardNum == cardNum){
                rIndex = i;
                break;
            }
        }
        return rIndex;
    };

    //返回是否存在 排除indexs里的
    that.findCardForNumApartIndexs = function(cards, cardNum, indexs){
        var rIndex = null;
        for (var i = 0; i < cards.length; i++){
            var isContinue = false;
            for (var j = 0; j < indexs.length; j++){
                if (i == indexs[j]){
                    isContinue = true;
                    break;
                }
            }
            if (isContinue){
                continue;
            }

            if (cards[i].mCardNum == cardNum){
                rIndex = i;
                break;
            }
        }
        return rIndex;
    };

    that.getCardNumIsBasicLaw = function(cardNum){
        var isExist = false;
        for (var i = 0; i < that.allBasicLawCardNums.length; i++){
            num = that.allBasicLawCardNums[i];
            if (cardNum === num){
                isExist = true;
                break;
            }
        }
        return isExist;
    };

    //找到赖子对应的牌
    that.getLzDecryptCardNums = function(groupType, cardNums, lzNum){
        var result = [];
        //从小到大排序
        cardNums.sort();

        if (lzNum == 1){
            if (groupType == 1){
                if ((cardNums[0] + 1) == cardNums[1]){
                    var lastNum = (cardNums[0] - 1);
                    if (that.getCardNumIsBasicLaw(lastNum)){
                        result.push(cardNums[0] - 1);
                    }

                    lastNum = (cardNums[1] + 1);
                    if (that.getCardNumIsBasicLaw(lastNum)){
                        result.push(cardNums[1] + 1);
                    }
                }else{
                    result.push(cardNums[0] + 1);
                }
            }else{
                result.push(cardNums[0]);
            }
        }
        return result;
    };

    //返回买6马中的值
    that.getMaimaZhong = function(maimaNums, winCard){
        var zhongMaNums = [];

        if (maimaNums.length > 0){
            if (mo.gameHelper.findValueForArray([21, 24, 27, 41, 44, 47, 85], winCard) != null){
                for (var i = 0; i < maimaNums.length; i++){
                    var num = maimaNums[i];
                    if (num == 85){
                        zhongMaNums.push(num);
                    }else{
                        var v = num % 20;
                        if (v == 1 || v == 4 || v == 7 || num > 80) {
                            zhongMaNums.push(num);
                        }
                    }
                }
            }else if (mo.gameHelper.findValueForArray([22, 25, 28, 42, 45, 48, 86], winCard) != null){
                for (var i = 0; i < maimaNums.length; i++){
                    var num = maimaNums[i];
                    if (num == 86){
                        zhongMaNums.push(num);
                    }else{
                        var v = num % 20;
                        if (v == 2 || v == 5 || v == 8 || num > 80) {
                            zhongMaNums.push(num);
                        }
                    }
                }
            }else if (mo.gameHelper.findValueForArray([23, 26, 29, 43, 46, 49, 87], winCard) != null){
                for (var i = 0; i < maimaNums.length; i++){
                    var num = maimaNums[i];
                    if (num == 87){
                        zhongMaNums.push(num);
                    }else{
                        var v = num % 20;
                        if (v == 3 || v == 6 || v == 9 || num > 80) {
                            zhongMaNums.push(num);
                        }
                    }
                }
            }
        }

        if (zhongMaNums.length == 0){
            zhongMaNums = maimaNums;
        }
        return zhongMaNums;
    };

    //返回表情序列帧
    that.getFaceFrames = function (index) {
        var spriteFrames = [];
        for (var i = 0; i <= 60; i++){
            var frame = cc.spriteFrameCache.getSpriteFrame("emoji_" + index + "_" + i + ".png");
            if (frame == null){
                break;
            }
            spriteFrames.push(frame);
        }
        return spriteFrames;
    };

    return that;
};

mo.kwxCardHelper = new KwxCardHelper();